import axios from 'axios'
import router from '../router/index'
import { ElMessage } from 'element-plus'
const baseURL = '/api'
const instance = axios.create({
  baseURL,
  timeout: 100000
})

instance.interceptors.request.use(
  (config) => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8'
    // config.headers['token'] = user.token;  // 设置请求头
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  (response) => {
    if (response.data.code === 1 || response.data.code === 0) {
      return response
    }
    return Promise.reject(response)
  },
  async (error) => {
    if (error.response?.status === 401) {
      ElMessage.error('您还未登录')
      await router.push('/login')
    }
  }
)

export default instance
export { baseURL }
